<?php
require_once 'Zend/Db/Table/Abstract.php';
class Model_DishLocationMapper extends Zend_Db_Table_Abstract {
	protected $_name = 'dish_location';
	protected $_primary='Dish_LocID';
	protected $_referenceMap = array (
			'dish' => array (
					'columns' => array ('DishID'),
					'refTableClass' => 'Model_DishMapper',
					'refColumns' => array ('DishID')
			),
			'location' => array (
					'columns' => array ('LocID'),
					'refTableClass' => 'Model_LocationMapper',
					'refColumns' => array ('LocID')
			)
	);
	
	public function getDishLocation($id){
		
		$row=$this->fetchRow($this->select()->where('Dish_LocID=?',$id));
		$result= Array();
		if ($row)
		{
			$result['Id']=$row->Dish_LocID;
			$result['Name']=$row->Name;
			$result['Price']=$row->Price;
		}
		return $result;
	}
	
	public function getAllLocationOfDish($dishId) {
		$result = Array ();
		
		$dishM = new Model_DishMapper ();
		$dish = $dishM->getDish ( $dishId );
		$i = 0;
		
		$rowset = $this->fetchAll ( $this->select ()->where ( 'DishID=?', $dishId ) )->toArray ();
		$rowCount = count ( $rowset );
		if ($rowCount > 0) {
			foreach ( $rowset as $row ) {
				$item = new Model_LocationOfDish ();
				$id= $row['Dish_LocID'];
				$price = $row ['Price'];
				$quantity = $row ['Quantity'];
				$image=$row['Image'];
				$des=$row['Description'];
				$locationId = $row ['LocID'];
				$name=$row['Name'];
				$locMapper = new Model_LocationMapper ();
				$loc = $locMapper->getLocation ( $locationId );
				$item->setLocation ( $loc )->setId($id)->setPrice ( $price )->setQuantity ( $quantity )->setImage($image)->setDescription($des)->setName($name);
				$result [$i] = $item;
				$i = $i + 1;
			}
		}
		
		return $result;
	}
	public function getAllDishOfLocation($locId, $loc) {
		$result = Array ();
		
		$locM = new Model_LocationMapper ();
		$location = $locM->getLocation ( $locId );
		$i = 0;
		
		$rowset = $this->fetchAll ( $this->select ()->where ( 'LocID=?', $locId ) )->toArray ();
		$rowCount = count ( $rowset );
		if ($rowCount > 0) {
			foreach ( $rowset as $row ) {
				$item = new Model_LocationOfDish ();
				$id=$row['Dish_LocID'];
				$price = $row ['Price'];
				$quantity = $row ['Quantity'];
				$dishId = $row ['DishID'];
				$image=$row['Image'];
				$des=$row['Description'];
				$name= $row['Name'];
				$dishMapper = new Model_DishMapper ();
				$dish = $dishMapper->getDish ( $dishId );
				$item->setDish ( $dish )->setId($id)->setName($name)-> setPrice ( $price )->setQuantity ( $quantity )->setImage($image)->setDescription($des);
				$item->setLocation ( $loc );
				$result [$i] = $item;
				$i = $i + 1;
			}
		}
		
		return $result;
	}
	
	public function getLocationByDishLocation($dishId)
	{
		$row=$this->fetchRow($this->select()->where('Dish_LocID=?',$dishId));
		if ($row)
		{
			$locId=$row->LocID;
			$locMapper=new Model_LocationMapper();
			$loc=$locMapper->getLocation($locId);
			return $loc;
		}
		return null;
		
	}
	
}